home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / net / bind-contrib.tar.gz / bind-contrib.tar / contrib / lamers / lamers.sh next >
Linux/UNIX/POSIX Shell Script  |  1996-10-25  |  11KB  |  339 lines

  1. #!/bin/sh
  2. #lamers.sh,v 1.1.1.1 1995/12/08 03:45:02 kim Exp
  3. #
  4. # From: Kimmo Suominen <kim@tac.nyc.ny.us>
  5. #
  6. # Lame delegation notifier
  7. #
  8. # -----------------------------------------------------------------------
  9. # COPYRIGHT FOR THE ORIGINAL SCRIPT
  10. # -----------------------------------------------------------------------
  11. #  Copyright (c) 1991, 1994 Regents of the University of Michigan.
  12. #  All rights reserved.
  13. #
  14. #  Redistribution and use is permitted provided that this notice 
  15. #  is preserved and that due credit is given to the University of 
  16. #  Michigan. The name of the University may not be used to endorse 
  17. #  or promote products derived from this software without specific 
  18. #  prior written permission. This software is provided "as is" 
  19. #  without express or implied warranty.
  20. #
  21. #  Original author:  Bryan Beecher
  22. # -----------------------------------------------------------------------
  23.  
  24. # -----------------------------------------------------------------------
  25. # To use this software, you will need to have built BIND 4.9.3 or a later
  26. # version) with LAME_DELEGATION defined in options.h.  You will also need
  27. # to be sure that the 'dig' program resides in the current PATH.
  28. # -----------------------------------------------------------------------
  29.  
  30. # -------------------------------------------------------------
  31. #  tailor these to your system
  32. # -------------------------------------------------------------
  33.  
  34. ##
  35. ##  Where are lame delegations recorded?
  36. ##
  37. LOGFILE="@LOGFILE@"
  38.  
  39. ##
  40. ##  Where should scratch files be kept?
  41. ##
  42. TMP="@TEMPDIR@"
  43.  
  44. ##
  45. ##  Who should get the main "lame delegations" report?
  46. ##
  47. ADMIN="@ADMIN@"
  48.  
  49. ##
  50. ##  Mailer to use
  51. ##
  52. MAILER="@MAILER@"
  53.  
  54. ##
  55. ##  Mail should show up as having come from this address
  56. ##
  57. SENDER="@SENDER@"
  58.  
  59. # -------------------------------------------------------------
  60. #  you shouldn't need to change any of these
  61. # -------------------------------------------------------------
  62. LAMERS=${TMP}/lamers$$
  63. LAMEREPORT=${TMP}/.lamereport$$
  64. REPORT=${TMP}/.report.$$
  65.  
  66. # -------------------------------------------------------------
  67. #  handle arguments
  68. # -------------------------------------------------------------
  69. #    -f <logfile>
  70. #    Change the LOGFILE.
  71. #
  72. #    -v
  73. #    Be verbose.
  74. #
  75. #    -e
  76. #    Echo messages that would have been sent as e-mail.
  77. #
  78. #    -t
  79. #    Test mode.  Do not send mail to the lame delegation
  80. #    hostmasters.
  81. #
  82. #    -a <admin>
  83. #    Specify a different admin.
  84. # -------------------------------------------------------------
  85. VERBOSE=0
  86. TESTMODE=0
  87. while [ $# != 0 ] ; do
  88.     case "$1" in
  89.  
  90.         -e)
  91.         MAILER=/bin/cat
  92.         ;;
  93.  
  94.         -f)
  95.         LOGFILE=$2
  96.         shift
  97.         ;;
  98.  
  99.         -v)
  100.         VERBOSE=1
  101.         ;;
  102.  
  103.         -t)
  104.         TESTMODE=1
  105.         ;;
  106.  
  107.         -a)
  108.         ADMIN=$2
  109.         shift
  110.         ;;
  111.     esac
  112.     shift
  113. done
  114.  
  115. #--------------------------------------------------------------------------
  116. #  Clean up and exit on a HUP, INT or QUIT
  117. #--------------------------------------------------------------------------
  118. trap "rm -f ${LAMERS} ${LAMEREPORT} ; exit" 1 2 3
  119.  
  120. if [ ${TESTMODE} -eq 1 ] ; then
  121.     echo "Operating in test mode."
  122.     echo "Notifications sent via: ${MAILER}"
  123. fi
  124.  
  125. #--------------------------------------------------------------------------
  126. #  See if there are any lamers
  127. #
  128. # Nov 30 04:20:17 grendel named[758]:
  129. # Lame server on 'ifcsun1.ifisiol.unam.mx' (in 'ifisiol.UNAM.MX'?):
  130. # [132.248.1.3].53 'danzon.astroscu.unam.mx':
  131. # learnt (A=192.33.4.12,NS=132.248.1.3)
  132. #
  133. #--------------------------------------------------------------------------
  134. grep "Lame server on" ${LOGFILE} | grep -v "*" | tr '[A-Z]' '[a-z]' |
  135.     sed -e "s/[^']*'\([^']*\)'[^']*'\([^']*\)'[^[]*\[\([^]]*\).*/\2 \3/" |
  136.     sort | uniq > ${LAMERS}
  137.  
  138. if [ ! -s ${LAMERS} ] ; then
  139.     exit 0
  140. fi
  141.  
  142. if [ ${VERBOSE} -eq 1 ] ; then
  143.     echo "Found" `awk 'END { print NR }' ${LAMERS}` "lame delegations"
  144. fi
  145.  
  146. #--------------------------------------------------------------------------
  147. #  There were lamers; send them mail
  148. #--------------------------------------------------------------------------
  149. touch ${LAMEREPORT}
  150. NAME=""
  151. while read DOMAIN IPADDR ; do
  152.     #-----------------------------------------------------------
  153.     # Echo args if verbose
  154.     #-----------------------------------------------------------
  155.     if [ ${VERBOSE} -eq 1 ] ; then
  156.         echo ""
  157.         echo "-> ${IPADDR} may be a lame delegation for ${DOMAIN}"
  158.     fi
  159.     #-----------------------------------------------------------
  160.     # Lookup the SOA record form ${DOMAIN}.  A really broken name
  161.     # server may have more than one SOA for a domain, so exit
  162.     # after finding the first one.  Send it to the local hostmaster
  163.     # if we cannot find the proper one.
  164.     #-----------------------------------------------------------
  165.     if [ ${VERBOSE} -eq 1 ] ; then
  166.         echo "   Looking up the hostmaster for ${DOMAIN}"
  167.     fi
  168.     HOSTMASTER=`dig ${DOMAIN} SOA 2> /dev/null |
  169.         awk '$3 == "SOA" { print substr($5, 1, length($5) - 1) ; exit }' |
  170.         sed -e 's/@/./' | sed -e 's/\./@/'`
  171.     NAME=`dig -x ${IPADDR} 2> /dev/null |
  172.         awk '$3 == "PTR" { print substr($4, 1, length($4) - 1) ; exit }'`
  173.     if [ -z "${HOSTMASTER}" ] ; then
  174.         if [ ! -z ""${NAME} ] ; then
  175.         HOSTMASTER="postmaster@${NAME}"
  176.         else
  177.         HOSTMASTER=""
  178.         fi
  179.     fi
  180.     HOSTMASTER="`echo ${HOSTMASTER} | tr '[A-Z]' '[a-z]'`"
  181.     if [ ${VERBOSE} -eq 1 -a -z "${HOSTMASTER}" ] ; then
  182.         echo "   Could not locate an appropriate e-mail address"
  183.     elif [ ${VERBOSE} -eq 1 ] ; then
  184.         echo "   "Hostmaster is "${HOSTMASTER}"
  185.     fi
  186.     #-----------------------------------------------------------
  187.     # Find the name associated with IP address ${IPADDR}.  Query
  188.     # the nameserver at that address:  If it responds listing
  189.     # itself as a domain namserver, then it is lame; if it isn't
  190.     # in the list, then perhaps the lame delegation alert was
  191.     # spurious.
  192.     #-----------------------------------------------------------
  193.     if [ ${VERBOSE} -eq 1 ] ; then
  194.         echo -n "   Is ${IPADDR} listed as a NS for ${DOMAIN}?  "
  195.     fi
  196.     dig @${IPADDR} ${DOMAIN} NS 2>&1 | grep "A    ${IPADDR}" > /dev/null
  197.     if [ $? -eq 1 ] ; then
  198.         if [ ${VERBOSE} -eq 1 ] ; then
  199.             echo "No, skipping."
  200.         fi
  201.         continue
  202.     else
  203.         if [ ${VERBOSE} -eq 1 ] ; then
  204.             echo "Yes."
  205.         fi
  206.     fi
  207.     #-----------------------------------------------------------
  208.     # If the delegation is no longer lame, don't send mail.
  209.     # We do the query twice; the first answer could be authori-
  210.     # tative even if the nameserver is not performing service
  211.     # for the domain.  If this is the case, then the second
  212.     # query will come from cached data, and will be exposed
  213.     # on the second query.  If the resolver returns trash, the
  214.     # entire set of flags will be set.  In this case, don't
  215.     # count the answer as authoritative.
  216.     #-----------------------------------------------------------
  217.     if [ ${VERBOSE} -eq 1 ] ; then
  218.         echo -n "   Data returned from ${IPADDR} is from the "
  219.     fi
  220.     dig @${IPADDR} ${DOMAIN} > /dev/null
  221.     dig @${IPADDR} ${DOMAIN} | grep flags | grep aa | grep -v tc > /dev/null
  222.     if [ $? -eq 0 ] ; then
  223.         if [ ${VERBOSE} -eq 1 ] ; then
  224.             echo "hash table (authoritative)."
  225.         fi
  226.         continue
  227.     fi
  228.     if [ ${VERBOSE} -eq 1 ] ; then
  229.         echo "cache (non-authoritative)."
  230.     fi
  231.     #-----------------------------------------------------------
  232.     # Notify the owner of the lame delegation, and also notify
  233.     # the local hostmaster.
  234.     #-----------------------------------------------------------
  235.     if [ -z "${HOSTMASTER}" ] ; then
  236.         continue
  237.     fi
  238.     if [ ${VERBOSE} -eq 1 ] ; then
  239.         echo "  "
  240.     fi
  241.     echo "${IPADDR}:${DOMAIN}:${NAME}:${HOSTMASTER}" >> ${LAMEREPORT}
  242. done < ${LAMERS}
  243.  
  244. #--------------------------------------------------------------------------
  245. # Message leader and trailer
  246. #--------------------------------------------------------------------------
  247. doleader() {
  248.     rm -f ${REPORT}
  249.     cat <<!EOF_LEADER >> ${REPORT}
  250. From: ${SENDER}
  251. To: $1
  252. Subject: Lame delegation report
  253.  
  254. This message was machine generated.  It is intended to alert you
  255. to a possible problem with the nameservers for the domains listed
  256. below.  If you have questions about this message, please contact
  257. ${SENDER}
  258.  
  259. This nameserver  was found to be a lame delegation for this domain
  260. ---------------  -------------------------------------------------
  261. !EOF_LEADER
  262. }
  263. AWKFMT="awk -F: '{ printf("'"%-16s %s\n", $1, $2) }'"'"
  264.  
  265. dotrailer() {
  266.     if [ -s ${REPORT} ] ; then
  267.         cat <<!EOF_TRAILER >> ${REPORT}
  268.  
  269. The following paragraphs describe what a lame delegation is and suggests
  270. some things you might do to eliminate the lame delegation.  If you are
  271. an experienced hostmaster, you probably do not to read the rest of this
  272. message; if you don't know what a lame delegation is, you probably want
  273. to keep reading.
  274.  
  275. A lame delegation is an instance when a nameserver has been listed as
  276. authoritative for a domain, but does not seem to be performing
  277. nameservice for that domain.  A lame delegation can usually be easily
  278. remedied by either fixing some small error in a configuration file, or
  279. by contacting the appropriate hostmaster and supplying him/her with
  280. up-to-date information.
  281.  
  282. Two pieces of information are listed above.  The first one is the name
  283. of the domain that was flagged, and the second one is the lame
  284. delegation -- that is, it is a purported nameserver for the domain.
  285.  
  286. If the nameserver should be performing nameservice for the listed
  287. domain, it may be useful to inspect it.  Is the zone for that domain
  288. expiring?  If the nameserver is performing secondary service, does it
  289. have the correct IP address of the master server(s)?  Again, if the
  290. nameserver is performing secondary service, is its copy of the SOA
  291. record serial number greater than the one held by the master server? Is
  292. there a typographical error or omission in the boot file?
  293.  
  294. If the nameserver should not be listed as a nameserver for the domain,
  295. contact the hostmaster for that domain and ask that the NS record
  296. pointing to your nameserver be deleted.  If your machine is not a
  297. nameserver for the domain, it should not be listed as such.
  298.  
  299. Thank you for your time and your patience.
  300. !EOF_TRAILER
  301.         ${MAILER} < ${REPORT}
  302.     fi
  303.     rm -f ${REPORT}
  304. }
  305. #--------------------------------------------------------------------------
  306. # Tell hostmasters
  307. #--------------------------------------------------------------------------
  308. OHM="nobody"
  309. if [ ${TESTMODE} -eq 0 ] ; then
  310.     sort -t: +3 -4 ${LAMEREPORT} | while read LAMELINE ; do
  311.         OIFS="${IFS}"
  312.         IFS=:
  313.         set x ${LAMELINE}
  314.         IFS="${OIFS}"
  315.         if [ "$5" != "${OHM}" ] ; then
  316.             dotrailer
  317.             doleader "$5"
  318.             OHM="$5"
  319.         fi
  320.         echo "${LAMELINE}" | eval ${AWKFMT} >> ${REPORT}
  321.     done
  322.     dotrailer
  323. fi
  324.  
  325. #--------------------------------------------------------------------------
  326. # No news is good news
  327. #--------------------------------------------------------------------------
  328. if [ -s ${LAMEREPORT} ] ; then
  329.     doleader "${ADMIN}"
  330.     sort -t. -n +0 -1 +1 -2 +2 -3 +3 -4 ${LAMEREPORT} |
  331.         eval ${AWKFMT} >> ${REPORT}
  332.     dotrailer
  333. fi
  334.  
  335. #--------------------------------------------------------------------------
  336. # Tidy up
  337. #--------------------------------------------------------------------------
  338. rm -f ${LAMERS} ${LAMEREPORT} ${REPORT}
  339.